package com.example.manualManagement.service;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.example.manualManagement.entity.User;
import com.example.manualManagement.mapper.UserMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;


@Service
public class UserService {

    @Autowired
    private UserMapper userMapper;

    public List<User> findByName(String userName) {
        QueryWrapper<User> wrapper=new QueryWrapper<>();
        wrapper.eq("userName",userName);

        List<User> user = userMapper.selectList(wrapper);
        return user;
    }

    public List<User> queryPage(Integer page) {
        int pageNow = page == null ? 1 : page;
        int pageSize = 5;
        int startRows = pageSize*(pageNow-1);
        long jiewei=startRows+ pageSize;
        long n=userMapper.selectCount(null);
        if(jiewei>n) {
            jiewei=n;
        }
        List<User> userPage=userMapper.selectList(null);
        return userPage.subList( startRows,(int)jiewei);
    }

    public long getRowCount() {
        return userMapper.selectCount(null);
    }

    public int insertUser(User user) {

        return userMapper.insert(user);
    }

    public List<User> ListUser(){
        return userMapper.selectList(null);
    }

    public int Update(User user){
        return userMapper.updateById(user);
    }

    public int delete(int userId){

        QueryWrapper<User> wrapper=new QueryWrapper<>();
        wrapper.eq("userId",userId);
        return userMapper.delete(wrapper);
    }

}